package cn.xyinping.user.controller;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.user.dto.LoginDto;
import cn.xyinping.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 27266
 * @desrciption 员工登陆接口
 * @date 2021/10/30
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = loginService.account(loginDto);  // 登陆成功把员工信息存到redis中  token: loginInfo
            // 把map返回给浏览器端
            return AjaxResult.me().setResultObj(map);  // setResultObj 向页面浏览器端传参数用
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

    /**
     * 微信登陆接口
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String> map) {
        try {
            AjaxResult result = loginService.wechat(map);
            return result;

        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

    /**
     * 绑定接口
     *
     * @param map
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String, String> param) {
        try {
            Map<String, Object> map1 = loginService.binder(param);
            return AjaxResult.me().setResultObj(map1);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }

    }
}
